<template>
  <div class=''>

    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card class="box-card">

      <!-- 搜索区 -->
      <el-row>
        <el-col :span='8'>
          <el-input
          v-model="queryInfo.query" placeholder="请输入内容"
          clearable @clear="getOrdersList"
          >
            <el-button slot="append" icon="el-icon-search" @click="getOrdersList"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 订单列表区域 -->
      <el-table :data="ordersList" border stripe>
        <el-table-column type="index" label="#" />

        <el-table-column label="订单编号" prop="order_number" />

        <el-table-column label="订单价格" prop="order_price"  width="110px" />

        <el-table-column label="是否付款" width="90px">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.pay_status ==='1'">已付款</el-tag>
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>

        <el-table-column label="是否发货" prop="is_send" width="90px" />

        <el-table-column label="下单时间" width="160px">
          <template slot-scope="scope">
          {{scope.row.create_time | dataFormat}}
          </template>
        </el-table-column>

        <el-table-column label="操作" width="120px">
          <template slot-scope="scope">
              <!-- 修改按钮 -->
              <el-button
              type="primary" icon="el-icon-edit" size="mini"
              @click="addressDialogVisible = true"
              ></el-button>
              <!-- 定位按钮 -->
              <el-button
              type="success" icon="el-icon-location" size="mini"
              @click="showProgressBox(scope.row)"
              ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[1, 5, 10, 20]"
        :page-size="queryInfo.pagesize"
        @size-change="handleSizeChange"
        :current-page="queryInfo.pagenum"
        @current-change="handleCurrentChange"
        :total="total">
      </el-pagination>
    </el-card>

    <!-- 修改地址的对话框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addressDialogVisible"
      width="50%"
      >

      <!-- 内容主体区域 -->
      <el-form
      :model="addressFrom"
      :rules="addressFromRules"
      ref="addressFromRef"
      label-width="100px"
      >
        <!-- 选择商品分类的级联选择框 -->
        <el-form-item label="省市区/县:" prop="address1">
          <el-cascader
            v-model="addressFrom.address1"
            :options="cityData"
            :show-all-levels="false"
            clearable
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址:" prop="address2">
          <el-input v-model="addressFrom.address2"></el-input>
        </el-form-item>
      </el-form>

      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addressDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 展示物流的对话框 -->
    <el-dialog
      title="物流进度"
      :visible.sync="progressDialogVisible"
      width="50%"
      >

      <!-- 内容主体区域 -->
      <el-timeline :reverse="false">
        <el-timeline-item
          v-for="(activity, index) in progressInfo"
          :key="index"
          :timestamp="activity.time">
          {{activity.context}}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import cityData from './cityData.js'

export default {
  name: 'Orders',
  props: {

  },
  data () {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      ordersList: [],
      // 与修改地址相关
      addressDialogVisible: false,
      addressFrom: {
        address1: [],
        address2: ''
      },
      addressFromRules: {
        address1: [
          { required: true, message: '请输入省市区/县', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ]
      },
      // 与联级有关表单
      cityData,
      // 与展示物流进度相关
      progressDialogVisible: false,
      progressInfo: []

    }
  },
  created () {
    this.getOrdersList()
  },
  methods: {
    // 获取订单列表
    async getOrdersList () {
      const { data: res } = await this.$http.get('orders', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单列表失败')
      }
      this.ordersList = res.data.goods
      this.total = res.data.total
    },

    // 监听 pagesize 改变事件
    handleSizeChange (NewSize) {
      this.queryInfo.pagesize = NewSize
      this.getOrdersList()
    },

    // 监听 页码值 改变的事件
    handleCurrentChange (NewPage) {
      this.queryInfo.pagenum = NewPage
      this.getOrdersList()
    },

    // 获取定位信息
    async showProgressBox (role) {
      // 发起获取网络请求
      const id = 1106975712662
      const { data: res } = await this.$http.get(
        `/kuaidi/${id}`
      )
      if (res.meta.status !== 200) {
        return this.$message.error('获取物流信息失败')
      }
      this.progressInfo = res.data
      // 展示对话框
      this.progressDialogVisible = true
    }
  }
}
</script>
<style  scoped>
.el-breadcrumb{
  margin-bottom: 15px;
  font-size: 12px;
}
.box-card{
  box-shadow:  0 1px 1px rgba(0, 0, 0, 0.15) !important;
}
.el-table{
  font-size: 12px;
  margin: 15px 0;
}
.el-cascader {
    display: block;
    position: relative;
    font-size: 14px;
    line-height: 40px;
}
</style>
